package com.lzy.pattern.factory.config_factory;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * @ClassName: CoffeeFactory
 * @Description: TODO
 * @Author: LiuZy
 * @Date: 2022/4/15 21:15
 * @Version: 1.0
 */
public class CoffeeFactory {

    private static Map<String, Coffee> map = new HashMap<>();

    static {
        Properties properties = new Properties();

        try {
            properties.load(CoffeeFactory.class.getClassLoader().getResourceAsStream("bean.propertites"));

            Set<Object> keys = properties.keySet();
            for (Object key : keys) {
                String claasName = properties.getProperty((String) key);
                Class<?> clazz = Class.forName(claasName);
                Coffee coffee = (Coffee) clazz.newInstance();
                map.put((String) key, coffee);
            }

            System.out.println(properties.getProperty("american"));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    public static Coffee createCoffee(String name) {
        return map.get(name);
    }

}